
package com.koo.common.utils;

import com.koo.common.exception.RRException;
import org.apache.commons.beanutils.*;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.StringUtils;

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.security.MessageDigest;
import java.text.DecimalFormat;
import java.text.MessageFormat;
import java.util.*;

public class MyStrUtil {
    private static final String algorithm = "MD5";
    public static String[] chars = new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

    public MyStrUtil() {
    }

    public static String generate16ShortUUID() {
        StringBuffer shortBuffer = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-", "");

        for(int i = 0; i < 16; ++i) {
            String str = uuid.substring(i * 2, i * 2 + 2);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[x % 62]);
        }
        return shortBuffer.toString();
    }

    public static String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public static String md5(String str) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(str.getBytes());
            byte[] b = md.digest();
            StringBuffer sb = new StringBuffer();

            for(int i = 0; i < b.length; ++i) {
                int v = b[i];
                v = v < 0 ? 256 + v : v;
                String cc = Integer.toHexString(v);
                if (cc.length() == 1) {
                    sb.append('0');
                }

                sb.append(cc);
            }

            return sb.toString();
        } catch (Exception var7) {
            return "";
        }
    }

    public static String[][] merge2D(String[][] twoDArray1, String[][] twoDArray2) {
        int size1 = 0;
        int size2 = 0;
        if (twoDArray1 != null) {
            size1 = twoDArray1.length;
        }

        if (twoDArray2 != null) {
            size2 = twoDArray2.length;
        }

        if (size1 + size2 == 0) {
            return (String[][])((String[][])null);
        } else {
            String[][] merge2DArray = new String[size1 + size2][2];

            int i;
            for(i = 0; i < size1; ++i) {
                merge2DArray[i][0] = twoDArray1[i][0];
                merge2DArray[i][1] = twoDArray1[i][1];
            }

            for(i = 0; i < size2; ++i) {
                merge2DArray[i + size1][0] = twoDArray2[i][0];
                merge2DArray[i + size1][1] = twoDArray2[i][1];
            }

            return merge2DArray;
        }
    }

    public static String[][] merge(String[][] sourceArray) {
        return sourceArray;
    }

    public static String[] convert2DTo1D(String[][] twoDArray) {
        if (twoDArray == null) {
            return null;
        } else {
            String[] result = new String[twoDArray.length];

            for(int i = 0; i < twoDArray.length; ++i) {
                result[i] = twoDArray[i][0];
            }

            return result;
        }
    }

    public static String[] merge1D(String[] oneDArray1, String[] oneDArray2) {
        int size1 = 0;
        int size2 = 0;
        if (oneDArray1 != null) {
            size1 = oneDArray1.length;
        }

        if (oneDArray2 != null) {
            size2 = oneDArray2.length;
        }

        String[] merge1DArray = new String[size1 + size2];

        int i;
        for(i = 0; i < size1; ++i) {
            merge1DArray[i] = oneDArray1[i];
        }

        for(i = 0; i < size2; ++i) {
            merge1DArray[i + size1] = oneDArray2[i];
        }

        return merge1DArray;
    }

    public static String[] merge1D(String[] oneDArray1, String stringToMerge) {
        String[] result;
        if (oneDArray1 == null) {
            result = new String[]{new String(stringToMerge)};
            return result;
        } else {
            result = new String[oneDArray1.length + 1];

            for(int i = 0; i < oneDArray1.length; ++i) {
                result[i] = oneDArray1[i];
            }

            result[oneDArray1.length] = stringToMerge;
            return result;
        }
    }

    public static String splitForIn(String[] oneDArray) {
        if (oneDArray != null && oneDArray.length != 0) {
            StringBuffer idStr = new StringBuffer("");
            String[] var2 = oneDArray;
            int var3 = oneDArray.length;

            for(int var4 = 0; var4 < var3; ++var4) {
                String str = var2[var4];
                idStr.append("'").append(str).append("',");
            }

            return idStr.substring(0, idStr.length() - 1);
        } else {
            return null;
        }
    }

    public static String splitListToStr(List<String> list, String delimiter) {
        if (list != null && list.size() != 0) {
            StringBuffer idStr = new StringBuffer("");

            for(int i = list.size() - 1; i > -1; --i) {
                String ss = (String)list.get(i);
                idStr.append(ss).append(delimiter);
            }

            return idStr.substring(0, idStr.length() - 1);
        } else {
            return null;
        }
    }

    public static String splitArrayToStr(Object[] arr, String delimiter) {
        String result = "";
        if (arr != null && arr.length > 0) {
            for(int i = 0; i < arr.length; ++i) {
                if (i + 1 == arr.length) {
                    result = result + arr[i];
                } else {
                    result = result + arr[i] + delimiter;
                }
            }
        }

        return result;
    }

    public static String getArrayStrByIndex(Object[] arr, int index, String delimiter) {
        String result = "";
        if (arr != null && arr.length > 0) {
            for(int i = 0; i <= index; ++i) {
                if (i == index) {
                    result = result + arr[i];
                } else {
                    result = result + arr[i] + delimiter;
                }
            }
        }

        return result;
    }

    public static void printObject(Object obj) {
        if (obj != null) {
            if (obj instanceof HashMap) {
                HashMap data = (HashMap)obj;
                Set keySet = data.keySet();
                Iterator var3 = keySet.iterator();

                while(var3.hasNext()) {
                    Object key = var3.next();
                    System.out.println(key + ":" + data.get(key));
                }
            } else if (obj instanceof ArrayList) {
                ArrayList data = (ArrayList)obj;
                System.out.println("ArrayList.size:" + data.size());
                Iterator var11 = data.iterator();

                while(var11.hasNext()) {
                    Object object = var11.next();
                    System.out.println();
                    printObject(object);
                }
            } else {
                int var15;
                int var16;
                if (obj.getClass().isArray()) {
                    Object[] data = (Object[])((Object[])obj);
                    Object[] var12 = data;
                    var15 = data.length;

                    for(var16 = 0; var16 < var15; ++var16) {
                        Object object = var12[var16];
                        printObject(object);
                    }
                } else if (obj instanceof String) {
                    System.out.print("    " + obj);
                } else {
                    Method[] methods = obj.getClass().getMethods();
                    Method[] var13 = methods;
                    var15 = methods.length;

                    for(var16 = 0; var16 < var15; ++var16) {
                        Method method = var13[var16];
                        if (method.getName().startsWith("get") && method.getParameterTypes().length == 0) {
                            try {
                                System.out.println(method.getName() + "--> " + method.invoke(obj));
                            } catch (Exception var7) {
                                var7.printStackTrace();
                            }
                        }
                    }
                }
            }

        }
    }

    public static Integer add(Integer int1, Integer int2) {
        return int1 + int2;
    }

    public static Integer subtract(Integer int1, Integer int2) {
        return int1 - int2;
    }

    public static BigDecimal add(BigDecimal int1, Integer int2) {
        return int1.add(BigDecimal.valueOf((long)int2));
    }

    public static Set<Integer> authStringToSet(String auth) {
        Set authSet = new HashSet();
        if (auth == null) {
            return authSet;
        } else {
            String[] auths = auth.split(",");

            for(int i = 0; i < auths.length; ++i) {
                authSet.add(Integer.parseInt(auths[i]));
            }

            return authSet;
        }
    }

    public static String authSetToString(Set<Integer> auth) {
        if (auth == null) {
            return "";
        } else {
            String authString = "";

            for(Iterator iter = auth.iterator(); iter.hasNext(); authString = authString + iter.next() + ",") {
            }

            if (authString.length() == 0) {
                return "";
            } else {
                authString = authString.substring(0, authString.length() - 1);
                return authString;
            }
        }
    }

    public static String message(String message, Object[] variables) {
        String output = MessageFormat.format(message, variables);
        return output;
    }

    public static String formatBDToS(BigDecimal value, String[] format) {
        if (value == null) {
            return null;
        } else {
            String DCformat = "##########0.00";
            if (format.length > 0 && !isEmpty(format[0])) {
                DCformat = format[0];
            }

            DecimalFormat decimalFormat = new DecimalFormat(DCformat);
            return decimalFormat.format(value);
        }
    }

    public static String setToString(Set<String> setMsg) {
        if (setMsg == null) {
            return " ";
        } else {
            String msgString = "";

            for(Iterator iter = setMsg.iterator(); iter.hasNext(); msgString = msgString + iter.next() + ", ") {
            }

            if (msgString.length() == 0) {
                return " ";
            } else {
                msgString = msgString.substring(0, msgString.length() - 2);
                return msgString;
            }
        }
    }

    public static String getClassName(Object obj) {
        if (obj == null) {
            return null;
        } else {
            String className = obj.getClass().getName();
            int index = className.lastIndexOf(".");
            return className.substring(index + 1);
        }
    }

    public static String getClassNameLowCase(Object obj) {
        return getClassName(obj).toLowerCase();
    }

    public static String messageNoErrCode(String message, Object[] variables) {
        String output = MessageFormat.format(message, variables);
        int index = output.indexOf(":");
        return index >= 0 && index <= 16 ? output.substring(index + 1) : output;
    }

    public static boolean isNull(String str) {
        return str == null;
    }

    public static boolean isEmpty(Object str) {
        if (str == null) {
            return true;
        } else {
            return "".equals(str.toString().trim()) || "NULL".equals(str.toString().trim()) || "null".equals(str.toString().trim());
        }
    }

    public static String getStrValue(String str) {
        String temp = "";
        if (!isEmpty(str)) {
            temp = str.trim();
        }

        return temp;
    }

    public static boolean isUpperCase(String value) {
        boolean isUpperCase = false;
        if (isNotEmpty(value)) {
            for(int i = 0; i < value.length(); ++i) {
                if (Character.isLowerCase(value.charAt(i))) {
                    return false;
                }
            }

            isUpperCase = true;
        }

        return isUpperCase;
    }

    public static boolean isNotEmpty(Object str) {
        if (str == null || str.toString() == "null") {
            return false;
        } else {
            return !"".equals(str.toString().trim());
        }
    }

    public static boolean isNum(String strIn) {
        String s = "1234567890";
        boolean b = true;
        if (strIn != null && !"".equals(strIn)) {
            for(int i = 0; i < strIn.length(); ++i) {
                if (s.indexOf(strIn.charAt(i)) < 0) {
                    b = false;
                    break;
                }
            }
            return b;
        } else {
            return false;
        }
    }

    public static String convertSpecialChars(String origin_text) {
        String convertedStr = origin_text != null ? origin_text : "";
        if (convertedStr.indexOf("\"") >= 0) {
            convertedStr = convertedStr.replaceAll("\"", "&quot;");
        }

        if (convertedStr.indexOf("'") >= 0) {
            convertedStr = convertedStr.replaceAll("'", "&#146;");
        }

        if (convertedStr.indexOf("<") >= 0) {
            convertedStr = convertedStr.replaceAll("<", "&lt;");
        }

        if (convertedStr.indexOf(">") >= 0) {
            convertedStr = convertedStr.replaceAll(">", "&gt;");
        }

        return convertedStr;
    }

    public static String genRandomStr(int pwd_len) {
        int count = 0;
        char[] str = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        StringBuffer pwd = new StringBuffer("");
        Random r = new Random();
        while(count < pwd_len) {
            int i = Math.abs(r.nextInt(36));
            if (i >= 0 && i < str.length) {
                pwd.append(str[i]);
                ++count;
            }
        }
        return pwd.toString();
    }

    public static String genRandomNum(int pwd_len) {
        int count = 0;
        char[] str = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        StringBuffer pwd = new StringBuffer("");
        Random r = new Random();

        while(count < pwd_len) {
            int i = Math.abs(r.nextInt(10));
            if (i >= 0 && i < str.length) {
                pwd.append(str[i]);
                ++count;
            }
        }

        return pwd.toString();
    }

    public static String toUtf8String(String s) {
        StringBuffer sb = new StringBuffer();

        for(int i = 0; i < s.length(); ++i) {
            char c = s.charAt(i);
            if (c >= 0 && c <= 255) {
                sb.append(c);
            } else {
                byte[] b;
                try {
                    b = Character.toString(c).getBytes("utf-8");
                } catch (Exception var7) {
                    System.out.println(var7);
                    b = new byte[0];
                }

                for(int j = 0; j < b.length; ++j) {
                    int k = b[j];
                    if (k < 0) {
                        k += 256;
                    }

                    sb.append("%" + Integer.toHexString(k).toUpperCase());
                }
            }
        }

        return sb.toString();
    }

    public static Object getArrayValue(Object[] arrays, int index) {
        return index >= arrays.length ? null : arrays[index];
    }

    public static String toSql(Object sour) {
        return toStr(sour).replaceAll("'", "''").replaceAll("[%\\[]", "");
    }

    public static String toSqlInsert(Object sour) {
        return toStr(sour).replaceAll("'", "''");
    }

    public static String toStr(Object sour) {
        return toStr(sour, "");
    }

    public static String toStr(Object sour, String init) {
        String dest = init;

        try {
            if (sour != null && sour.toString().trim().length() > 0) {
                dest = sour.toString();
            }
        } catch (Exception var4) {
            dest = init;
        }

        return dest.trim();
    }

    public static String right(String sour, int length) {
        if (sour == null) {
            return null;
        } else {
            return sour.length() > length ? sour.substring(sour.length() - length, sour.length()) : sour;
        }
    }

    public static String left(String sour, int length) {
        if (sour == null) {
            return null;
        } else {
            return sour.length() > length ? sour.substring(0, length) : sour;
        }
    }

    public static String elapsedDate(long millisecond) {
        String returnValue = "";
        long s = millisecond / 1000L;
        long y = s / 31536000L;
        s %= 31536000L;
        long d = s / 86400L;
        s %= 86400L;
        long h = s / 3600L;
        s %= 3600L;
        long m = s / 60L;
        s %= 60L;
        if (y > 0L) {
            returnValue = y + "年";
        }

        if (d > 0L) {
            returnValue = returnValue + d + "天";
        }

        if (h > 0L) {
            returnValue = returnValue + h + "时";
        }

        if (m > 0L) {
            returnValue = returnValue + m + "分";
        }

        if (s > 0L) {
            returnValue = returnValue + s + "秒";
        }

        return returnValue;
    }

    public static Integer toInt(Object sour, Integer init) {
        Integer dest = init;

        try {
            if (sour != null) {
                dest = Integer.parseInt(String.valueOf(sour));
            }
        } catch (Exception var4) {
            dest = init;
        }

        return dest;
    }

    public static int toInt(Object sour) {
        return toInt(sour, 0);
    }

    public static boolean isNumeric(String str) {
        if (str != null && str.length() != 0) {
            int i = str.length();

            char chr;
            do {
                --i;
                if (i < 0) {
                    return true;
                }

                chr = str.charAt(i);
            } while(chr >= '0' && chr <= '9');

            return false;
        } else {
            return false;
        }
    }

    public static float toFloat(Object sour) {
        return toFloat(sour, 0.0F);
    }

    public static float toFloat(Object sour, float init) {
        float dest = init;

        try {
            if (sour != null) {
                dest = Float.parseFloat(sour.toString());
            }
        } catch (Exception var4) {
            dest = init;
        }

        return dest;
    }

    public static short toShort(Object sour) {
        short dest = 0;

        try {
            if (sour != null) {
                dest = Short.parseShort(sour.toString());
            }
        } catch (Exception var3) {
            dest = 0;
        }

        return dest;
    }

    public static long toLong(Object sour, long init) {
        long dest = init;

        try {
            if (sour != null) {
                dest = Long.parseLong(sour.toString());
            }
        } catch (Exception var6) {
            dest = init;
        }

        return dest;
    }

    public static long toLong(Object sour) {
        return toLong(sour, 0L);
    }

    public static BigDecimal toBigDecimal(String sour) {
        if (isEmpty(sour)) {
            return BigDecimal.ZERO;
        } else {
            try {
                return new BigDecimal(sour);
            } catch (Exception var2) {
                System.out.println(sour + " :" + var2.getMessage());
                var2.printStackTrace();
                return BigDecimal.ZERO;
            }
        }
    }

    public static List removeDuplicate(List list) {
        HashSet h = new HashSet(list);
        list.clear();
        list.addAll(h);
        return list;
    }

    public static List removeDuplicateObj(List list) {
        Set set = new HashSet();
        List newList = new ArrayList();
        Iterator iter = list.iterator();

        while(iter.hasNext()) {
            Object element = iter.next();
            if (set.add(element)) {
                newList.add(element);
            }
        }

        return newList;
    }

    public static String[] delArrItemByIndex(String[] str, int index) {
        List<String> list = new ArrayList();

        for(int i = 0; i < str.length; ++i) {
            list.add(str[i]);
        }

        list.remove(index);
        String[] newStr = (String[])list.toArray(new String[1]);
        return newStr;
    }


    public static int getArrayIndexByValue(String[] strs, String value) {
        if (strs != null && strs.length > 0) {
            for(int i = strs.length - 1; i >= 0; --i) {
                if (value.equals(strs[i])) {
                    return i;
                }
            }
        }
        return -1;
    }

    public static String[] getArrayOfMergeSameValue(String[] arr) {
        String[] newArr = null;
        List<String> list = new ArrayList();
        if (arr != null && arr.length > 0) {
            String temp = null;
            String[] var4 = arr;
            int var5 = arr.length;

            for(int var6 = 0; var6 < var5; ++var6) {
                String str = var4[var6];
                if (isNotEmpty(str) && !str.equals(temp)) {
                    temp = str;
                    list.add(str);
                }
            }

            newArr = (String[])((String[])list.toArray(new String[list.size()]));
        }

        return newArr;
    }

    public static String[] getArrayOfRemoveItem(String[] arr, String val) {
        String[] newArr = null;
        List<String> list = new ArrayList();
        if (arr != null && arr.length > 0) {
            String[] var4 = arr;
            int var5 = arr.length;

            for(int var6 = 0; var6 < var5; ++var6) {
                String str = var4[var6];
                if (isNotEmpty(str) && !str.equals(val)) {
                    list.add(str);
                }
            }

            newArr = (String[])((String[])list.toArray(new String[list.size()]));
        }

        return newArr;
    }

    public static void sleep(int second) {
        try {
            Thread.currentThread();
            Thread.sleep((long)(second * 1000));
        } catch (Exception var2) {
        }

    }

    public static BigDecimal getRandomTemperature() {
        return new BigDecimal(String.format("%.2f", Math.random() * 30.0D));
    }

    public static BigDecimal getRandomHumidity() {
        return new BigDecimal(String.format("%.2f", 35.0D + Math.random() * 41.0D));
    }

    public static String getDecodeString(String encodeString) {
        return URLDecoder.decode(encodeString);
    }

    public static String encodeBase64(String data) {
        return Base64.encodeBase64String(data.getBytes());
    }

    public static String decodeBase64(String base64String) {
        return StringUtils.newStringUtf8(Base64.decodeBase64(base64String));
    }

    public static Object getProperty(Object object, String field) {
        try {
            return PropertyUtils.getProperty(object, field);
        } catch (Exception var3) {
            throw new RRException(object + "取" + field + "数据getProperty有误:" + var3.getMessage());
        }
    }
}
